package com.zj.sa_token.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.secure.SaSecureUtil;
import com.zj.sa_token.entity.UserInfoEntity;
import com.zj.sa_token.service.UserInfoService;
import com.zj.sa_token.util.avoid.AvoidRepeatableCommit;
import com.zj.sa_token.util.result.Result;
import com.zj.sa_token.util.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import webgis.tools.StringFacs;

import java.util.List;
import java.util.Map;

/**
 * @author li
 * @date 2024-01-12 11:03:58
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/userInfo")
@Api(value = "userInfo", tags = "userInfo管理模块")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Value("${config.application.publicKey}")
    private String publicKey;

    /**
     * 分页查询
     *
     * @param pageNum        分页对象
     * @param userInfoEntity
     * @return Result
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public Result getUserInfoget(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, UserInfoEntity userInfoEntity) {
        return ResultUtil.success(userInfoService.get(pageNum, pageSize, userInfoEntity));
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return Result
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return ResultUtil.success(userInfoService.getById(id));
    }

    /**
     * 新增 @SaIgnore不拦截
     *
     * @param
     * @return Result
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping
    @SaIgnore
    //aop切面防止短时间多次提交
    @AvoidRepeatableCommit(timeout=20)
    public Result save(@RequestParam("userName") String userName, @RequestParam("password") String password, @RequestParam("role") String role) {
        //时间戳作为id
        //        String l = String.valueOf(System.currentTimeMillis());
//        新增之前进行查询，用户名不能重复，记得校验
//        用户名、密码为空   记得校验
        if("".equals(userName) || "".equals(password)){
            return ResultUtil.error(-1,"用户名或密码不能为空！");
        }
        UserInfoEntity user = new UserInfoEntity();
        List<Map> list = userInfoService.getUserInfoByUserNameList(userName);
        if(list.size() > 0){
            return ResultUtil.error(-1,"已存在该用户！");
        }
        String guid32 = StringFacs.getGuid32();
        user.setId(guid32);
        user.setUserName(userName);
        user.setRole(role);
        String s_password = SaSecureUtil.rsaEncryptByPublic(publicKey, password);//公钥加密后的密码
        user.setPassword(s_password);
        return ResultUtil.success(userInfoService.save(user));
    }

    /**
     * 修改
     *
     * @param userInfoEntity
     * @return Result
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping
    public Result updateById(@RequestBody UserInfoEntity userInfoEntity) {
        return ResultUtil.success(userInfoService.updateById(userInfoEntity));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return Result
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id) {
        return ResultUtil.success(userInfoService.removeById(id));
    }
}

